package com.cn.wanxi.controller;


import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.entity.UserEntity;
import com.cn.wanxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/back/user")
public class UserController {

    //    @RequestMapping("/findById")
//    public String setUser(Model model) {
//        model.addAttribute("username", "aaaaa");
//        return "/index";
//    }
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public void findAll(UserEntity userEntity, HttpServletResponse resp) throws IOException {
        if (!"".equals(userEntity.getHobby()) && userEntity.getHobby() != null) {
            userEntity.setHobbys(userEntity.getHobby().split(","));
        }
        if (userEntity.getCurPage() == null) {
            userEntity.setCurPage(1);
        }
        List<UserEntity> list = userService.findAll(userEntity);
        int totalCount = userService.getCount(userEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("totalCount", totalCount);
        resp.getWriter().println(jsonObject);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public UserEntity findById(UserEntity userEntity, HttpServletResponse resp) throws IOException {
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("model", userService.findById(userEntity.getId()));
//        resp.getWriter().println(jsonObject);
        return userService.findById(userEntity.getId());
    }


}
